#! /bin/bash

# --------------------------------------------------------------
# Filename : recursion.sh
# Version  : "0.1"
# Date     : "2007-07-02 09:31:28"
# Author   : "Jiahuan Li <huanlf@gmail.com>"
# License  : "Licensed under the GPL v2"
# Copyright: "Copyright (C) 2007 by Jiahuan Li <huanlf@gmail.com>"
# 
# Description: to show how bash do recursion
# --------------------------------------------------------------

# set -e

rec() {
    local a=$1
    if [ ! -d "$a" ]; then
        echo "File: $(pwd)/$a"
    else
        cd "$a" 2>/dev/null && echo "Dir: `pwd`" || return
        local b
        for b in $( ls -1 ); do
            rec "$b";
        done
        cd ..
    fi
}

## main ##

if [ $1 ]; then
    rec $1
else
    rec "$HOME"
fi
